
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Teach,Ember-Route," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>loading、error子状态 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        loading、error子状态
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Route/" rel="tag">Ember-Route</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Teach/" rel="tag">Ember-Teach</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 4 分钟</span>
    </header>

    <div class="post-content">
      <p>在前面的<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/03/25/ember-js-ru-men-zhi-nan-zhi-er-shi-lu-you-ding-yi/">Ember.js 入门指南之二十路由定义</a>提过<code>loading</code>、<code>error</code>子路由，它们是<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>默认创建的，并在<code>beforeModel</code>、<code>model</code>、<code>afterModel</code>这三个回调执行完毕之前会先渲染当前路由的<code>loading</code>和<code>error</code>模板。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Router.map(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">this</span>.route(<span class="string">&#x27;posts&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">      <span class="built_in">this</span>.route(<span class="string">&#x27;post&#x27;</span>, &#123; <span class="attr">path</span>: <span class="string">&#x27;/:post_id&#x27;</span>&#125;);</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>对于上述的路由设置<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>会生成如下的路由列表：</p>
<p><img src="http://static.oschina.net/uploads/space/2015/0928/010633_x4BH_565401.png" alt="路由映射图"></p>
<p>每个路由都会自动生成一个<code>loading</code>、<code>error</code>路由，下面我将一一演示这两个路由的作用。<br>图片前面<code>loading</code>、<code>error</code>路由对应的<code>application</code>路由。<code>posts_loading</code>和<code>posts_error</code>对应的是<code>posts</code>路由。</p>
<h3 id="1，loading子状态"><a href="#1，loading子状态" class="headerlink" title="1，loading子状态"></a>1，loading子状态</h3><p><a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>建议数据放在<code>beforeModel</code>、<code>model</code>、<code>afterModel</code>回调中获取并传递到模板显示。但是只要是加载数据就需要时间，对于<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>应用来说，在<code>model</code>等回调中加载完数据才会渲染模板，如果加载数据比较慢那么用户看到的页面就是一个空白的页面，用户体验很差！</p>
<p><a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>提供的解决办法是：在<code>beforeModel</code>、<code>model</code>、<code>afterModel</code>回调还没返回前先进入一个叫<code>loading</code>的子状态，然后渲染一个叫<code>routeName-loading</code>的模板（如果是<code>application</code>路由则对应的直接是<code>loading</code>、<code>error</code>不需要前缀）。</p>
<p>为了演示这效果在<code>app/templates</code>下创建一个<code>posts-loading</code>模板。如果程序正常，在渲染模板<code>posts</code>之前会先渲染这个模板。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- app/templates/posts-loading.hbs --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">img</span> <span class="attr">src</span>=<span class="string">&quot;assets/images/loading/loading.gif&quot;</span> /&gt;</span></span><br></pre></td></tr></table></figure>
<p>然后修改路由<code>posts.js</code>，让<code>model</code>回调执行时间更长一些。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">//  模拟一个延时操作，</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">10000000</span>;i++) &#123;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> Ember.$.getJSON(<span class="string">&#x27;https://api.github.com/repos/emberjs/ember.js/pulls&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>执行<a target="_blank" rel="noopener" href="http://localhost:4200/posts">http://localhost:4200/posts</a>，首先会看到执行的<code>loading</code>模板的内容，然后才看到真正要显示的数据。有一个加载过程，如下面2幅图片所示。</p>
<p><img src="/content/images/2016/03/65.png" alt="图1"></p>
<p><img src="/content/images/2016/03/67.png" alt="图2"></p>
<h3 id="2，loading事件"><a href="#2，loading事件" class="headerlink" title="2，loading事件"></a>2，loading事件</h3><p>在<code>beforeModel</code>、<code>model</code>、<code>afterModel</code>回调没有立即返回之前，会先执行一个名为loading的事件。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">//  模拟一个延时操作，</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">10000000</span>;i++) &#123;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> Ember.$.getJSON(<span class="string">&#x27;https://api.github.com/repos/emberjs/ember.js/pulls&#x27;</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    actions: &#123;</span><br><span class="line">        loading: <span class="function"><span class="keyword">function</span>(<span class="params">transition, originRoute</span>) </span>&#123;</span><br><span class="line">            alert(<span class="string">&quot;Sorry this is taking so long to load!!&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>页面刷新后会弹出一个提示框，先不点击“确定”。打开浏览器的“开发者 -&gt; 开发者工具”，切换到Network标签下。找到“pulls”这个请求，点击它。</p>
<p><img src="http://static.oschina.net/uploads/space/2015/0928/012646_7ovz_565401.png" alt="result"></p>
<p>从图中可以看到此时<code>model</code>回调并没有返回。此时响应的内容是空的，说明<code>loading</code>事件实在<code>model</code>回调返回之前执行了。</p>
<p>然后点击弹出框的“确定”，此时可以看到Response有数据了。说明<code>model</code>回调已经执行完毕。<br>注意：如果当前的路由没有显示定义<code>loading</code>事件，这个时间会冒泡到父路由，如果父路由也没有显示定义<code>loading</code>事件，那么会继续向上冒泡，一直到最顶端的路由<code>application</code>。</p>
<h3 id="3，error子状态"><a href="#3，error子状态" class="headerlink" title="3，error子状态"></a>3，error子状态</h3><p>与<code>loading</code>子状态类似，<code>error</code>子状态会在<code>beforeModel</code>、<code>model</code>、<code>afterModel</code>回调执行过程中出现错误的时候触发。</p>
<p>命名方式与<code>loading</code>子状态也是类似的。现在定义一个名为<code>posts-error.hbs</code>的模板。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/posts-error.hbs --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">style</span>=<span class="string">&quot;color: red;&quot;</span>&gt;</span></span><br><span class="line">posts回调解析出错。。。。</span><br><span class="line"><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>然后在<code>model</code>回调中手动添加一个错误代码。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="comment">//  模拟一个延时操作，</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; <span class="number">10000000</span>;i++) &#123;</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">var</span> e = <span class="built_in">parseInt</span>(value);</span><br><span class="line">        <span class="keyword">return</span> Ember.$.getJSON(<span class="string">&#x27;https://api.github.com/repos/emberjs/ember.js/pulls&#x27;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>注意<code>var e = parseInt(value);</code>这句代码，由于<code>value</code>没有定义所以应该会报错。那么此时页面会显示什么呢？？</p>
<p><img src="/http://static.oschina.net/uploads/space/2015/0928/012833_n2HG_565401.png" alt="result"></p>
<p>如果你的演示程序没有其他问题那么你也会得到上图的结果。但是如果没有定义这个模板，那么界面上将是什么都不显示。</p>
<p>如果你想在<code>xxx-error.hbs</code>模板上看到是什么错误信息，你可以在模板上打印<code>model</code>的值。如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/posts-error.hbs --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">p</span> <span class="attr">style</span>=<span class="string">&quot;color: red;&quot;</span>&gt;</span></span><br><span class="line">posts回调解析出错。。。。</span><br><span class="line"><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">&#123;&#123;model&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>此时页面会显示出你的代码是什么错误。</p>
<p><img src="/content/images/2016/03/71.png" alt="result"></p>
<p>不过相比于浏览器控制台打印的错误信息简单很多！！！</p>
<h3 id="4，error事件"><a href="#4，error事件" class="headerlink" title="4，error事件"></a>4，error事件</h3><p><code>error</code>事件与第一点讲的loading事件也是相似的。使用方式与<code>loading</code>一样。个人觉得这个事件非常有用，我们可以在这个事件中根据<code>error</code>状态码的不同执行不同的逻辑，比如跳转到不同的路由上。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">    model: <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> Ember.$.getJSON(<span class="string">&#x27;https://api.github.com/repos/emberjs/ember.js/pulls____&#x27;</span>);</span><br><span class="line">    &#125;,</span><br><span class="line">    </span><br><span class="line">    actions: &#123;</span><br><span class="line">        error: <span class="function"><span class="keyword">function</span>(<span class="params">error, transition</span>) </span>&#123;</span><br><span class="line">            <span class="built_in">console</span>.log(<span class="string">&#x27;error = &#x27;</span> + error.status);</span><br><span class="line">            <span class="comment">//  打印error对象里的所有属性和方法名</span></span><br><span class="line">            <span class="keyword">for</span>(<span class="keyword">var</span> name <span class="keyword">in</span> error)&#123;         </span><br><span class="line">               <span class="built_in">console</span>.log(name); </span><br><span class="line">               <span class="comment">// console.log(&#x27;属性值或者方法体==》&#x27; + error[name]);</span></span><br><span class="line">            &#125;    </span><br><span class="line">            alert(names); </span><br><span class="line">            <span class="keyword">if</span> (error &amp;&amp; error.status === <span class="number">400</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="built_in">this</span>.transitionTo(<span class="string">&quot;about&quot;</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (error.status === <span class="number">404</span>) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="built_in">this</span>.transitionTo(<span class="string">&quot;form&quot;</span>);</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="built_in">console</span>.log(<span class="string">&#x27;else......&#x27;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>注意<code>getJSON</code>方法里的URL，我在URL后面随机加了一些字符，目的是让这个URL不存在。此时请求应该会找不到这个地址<code>error</code>的响应码应该是404。然后直接跳转到<code>form</code>这个路由上。<br>运行<a target="_blank" rel="noopener" href="http://localhost:4200/posts">http://localhost:4200/posts</a>之后，浏览器控制台打印信息如下：</p>
<p><img src="http://static.oschina.net/uploads/space/2015/0928/012833_n2HG_565401.png" alt="result"></p>
<p>页面也会跳转到<code>form</code>。</p>
<p><img src="/content/images/2016/03/73.png" alt="result"></p>
<p>到此路由加载数据过程中涉及的两个状态<code>loading</code>和<code>error</code>的内容全部介绍完，这两个状态在优化用户体验方面是非常有用的，希望想学习<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>的同学好好掌握！！！=^=</p>
<br>
博文完整代码放在[Github](https://github.com/ubuntuvim/my_emberjs_code)（博文经过多次修改，博文上的代码与github代码可能又出入，不过影响不大！），如果你觉得博文对你有点用，请在github项目上给我点个`star`吧。您的肯定对我来说是最大的动力！！

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/ember-teach/routing/%E6%8C%87%E5%AE%9A%E4%B8%8E%E8%B7%AF%E7%94%B1%E5%85%B3%E8%81%94%E7%9A%84%E6%A8%A1%E5%9E%8B/" rel="next" title="指定与路由关联的模型">
          指定与路由关联的模型
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/models/%E8%AE%BE%E7%BD%AE%E8%AE%B0%E5%BD%95%E5%88%B0Store/" rel="prev" title="设置记录到Store">
            设置记录到Store
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%EF%BC%8Cloading%E5%AD%90%E7%8A%B6%E6%80%81"><span class="toc-text">1，loading子状态</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%EF%BC%8Cloading%E4%BA%8B%E4%BB%B6"><span class="toc-text">2，loading事件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%EF%BC%8Cerror%E5%AD%90%E7%8A%B6%E6%80%81"><span class="toc-text">3，error子状态</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%EF%BC%8Cerror%E4%BA%8B%E4%BB%B6"><span class="toc-text">4，error事件</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: 'loading、error子状态', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
